export default {
  toggleFold(state, payload) {
    if (payload === false) {
      state.fold = false;
    } else if (payload === true) {
      state.fold = true;
    } else {
      state.fold = !state.fold
    }
  },
  titleChange(state, payload) {
    state.title = payload;
  },
  productChange(state, payload) {
    state.title = payload.title;
    if (payload.productId) {
      state.productId = payload.productId;
    }
    if (payload.goodsId) {
      state.goodsId = payload.goodsId;
    }
    if (payload.currency) {
      state.currency = payload.currency;
    }
  },
  showStyleChange(state, payload) {
    state.showStyle = payload;
  },
  chartShowChange(state, payload) {
    state.chartShow = payload;
  },
  productOrListChange(state, payload) {
    state.productOrList = payload;
  },
  totalProductsChange(state, payload) {
    state.totalProducts = payload;
  },
  changeActiveName(state, payload) {
    state.activeName = payload;
  },
  sortByChange(state, payload) {
    state.sortBy = payload;
  },
  keyWordsChange(state, payload) {
    state.keyWords = payload;
  },
  removeCountry(state, payload) {
    let index = state.selectCountry.findIndex(item => item.location_id === payload);
    state.selectCountry.splice(index, 1)
  },
  addCountry(state, payload) {
    state.selectCountry.push(payload)
  },
  removeCondition(state, payload) {
    let index = state.selectCondition.findIndex(item => item.id === payload);
    state.selectCondition.splice(index, 1)
  },
  addCondition(state, payload) {
    state.selectCondition.push(payload)
  },
  removeRetailer(state, payload) {
    let index = state.selectRetailer.findIndex(item => item.id === payload);
    state.selectRetailer.splice(index, 1)
  },
  addRetailer(state, payload) {
    state.selectRetailer.push(payload)
  },
  initRetailer(state, payload) {
    for (let i of payload) {
      state.selectRetailer.push(i)
    }
  },
  addBrands(state, payload) {
    state.selectBrands.push(payload);
    let models = payload.models;
    let selectCollectionId = state.selectCollection.map(item => item.id);
    models.forEach(element => {
      if (!selectCollectionId.includes(element.id)) {
        state.selectCollection.push(element);
      }
    });
  },
  initBrands(state, payload) {
    for (let i of payload) {
      i.models.forEach(i => i.value = true)
      state.selectBrands.push(i);
      state.selectCollection =  state.selectCollection.concat(i.models);
    }
  },
  removeBrands(state, payload) {
    let index = state.selectBrands.findIndex(item => item.id === payload);
    state.selectBrands.splice(index, 1)
    state.selectCollection = state.selectCollection.filter(item => item.brand_id !== payload)
  },
  removeMaterial(state, payload) {
    let index = state.selectMaterial.findIndex(item => item.id === payload);
    state.selectMaterial.splice(index, 1)
  },
  addMaterial(state, payload) {
    state.selectMaterial.push(payload)
  },
  removeColor(state, payload) {
    let index = state.selectColor.findIndex(item => item.id === payload);
    state.selectColor.splice(index, 1)
  },
  addColor(state, payload) {
    state.selectColor.push(payload)
  },
  removeClassification(state, payload) {
    let index = state.selectType.findIndex(item => item.id === payload);
    state.selectType.splice(index, 1)
  },
  addClassification(state, payload) {
    state.selectType.push(payload)
  },
  removeCollection(state, payload) {
    let index = state.selectType.findIndex(item => item.id === payload);
    state.selectCollection.splice(index, 1)
  },
  addCollection(state, payload) {
    state.selectCollection.push(payload)
  }
}
